<?php

/* 
 * Ajax类
 */
class Ajax{
    
    const STATUS_CODE_SUCCESS = 200;
    const STATUS_CODE_RAW_CONTENT = 201;
    const STATUS_CODE_FAILED = 300;
    const STATUS_CODE_CLIENT_ERROR = 400;
    const STATUS_CODE_SERVER_ERROR = 500;


    /**
     * ajax返回给HTTP请求的状态码, 如：200, 201, 300, 400, 500
     * @var int
     */
    public $status_code;


    /**
     * 错误信息
     * @var string or array
     */
    public $errors;


    /**
     * 警告信息
     * @var string or array
     */

    public $warnings;


    /**
     * ajax返回的信息
     * @var string or array
     */
    public $messages;


    /**
     * ajax返回的原生内容(非json)
     * @var string
     */
    public $content;


	/**
     * 查询结果数组
     * @var array
     */
    public $results;

    
    /**
     * Constructor.
     */
    public function __construct()
    {
        $this->status_code = self::STATUS_CODE_CLIENT_ERROR; //默认状态码

		//初始化将返回的数据
        $this->errors = '';
        $this->warnings = '';
        $this->messages = '';
        $this->content = '';
        $this->results = array();
    }


    /**
     * Render当前ajax对象
     */
    public function render()
    {
		//输出原生信息
        if($this->status_code == self::STATUS_CODE_RAW_CONTENT)
        {
            die($this->content);
        }

		$data = array();

        foreach(get_object_vars($this) as $key => $val)
        {
            if(!is_object($this->$key) && $key{0} != '_')
            {
				$data[$key] = $val;
            }
        }

		die(json_encode($data));
    }    

}
